package com.empire.module.member.service.miping;

import cn.hutool.core.util.StrUtil;
import com.empire.framework.common.encrypt.EncryptResponse;
import com.empire.framework.security.core.service.MockCryptoUtil;
import com.empire.framework.security.core.service.PkiVerifyApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MiPingService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Value("${spring.profiles.active:}")  // 读取激活的环境，默认空
    private String activeProfile;
    public boolean isProd() {
        return "prod".equals(activeProfile);
    }

    public String encrypt(String data) {
        String ciphertext = "";
        if (StrUtil.isNotEmpty(data)) {
            if (!isProd()) {
                // 开发环境
                EncryptResponse response = MockCryptoUtil.encrypt(data);
                String random = response.getData().getPucRandom();
                ciphertext = response.getData().getCiphertext();
                stringRedisTemplate.opsForValue().set(response.getData().getCiphertext(), random);
            }else {
                // 生产环境
                EncryptResponse response = PkiVerifyApiClient.encrypt(data);
                String random = response.getData().getPucRandom();
                ciphertext = response.getData().getCiphertext();
                stringRedisTemplate.opsForValue().set(response.getData().getCiphertext(), random);
            }
        }
        return ciphertext;
    }
}
